package com.dongke.controller;

import com.dongke.pojo.Role1;
import com.dongke.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("roleController")
public class roleController {

    @Autowired
    private RoleService roleService;

    @RequestMapping("toshow")
    public String toshow(){
        return "role/show";
    }

    //获取角色展示页面数据
    @RequestMapping("getInfo")
    @ResponseBody
    public Map<Object, Object> getInfo(Role1 role, @RequestParam(required = false, defaultValue = "3") Integer pageSize, @RequestParam(required = false, defaultValue = "1") Integer pageNum){
        HashMap<Object, Object> map = new HashMap<>();
        PageInfo<Role1> page = roleService.selectInfo(role, pageSize, pageNum);
        map.put("total", page.getTotal());
        map.put("rows", page.getList());
        return map;
    }

    @RequestMapping("toAdd")
    public String toAdd(){
        return "role/add";
    }
    //角色增加数据填充至数据库
    @RequestMapping("addRoleInfo")
    @ResponseBody
    public boolean addRoleInfo(Role1 role, @RequestParam("mids") Integer[] mids) {
         return roleService.addRoleInfo(role, mids);
    }

    @RequestMapping("deleteInfoById")
    @ResponseBody
    public boolean deleteInfoById(Integer rid) {
        boolean b = roleService.deleteInfoById(rid);
         return b;
    }

    @RequestMapping("toUpdate")
    public String toUpdate(Model model,Integer id){
         Role1 role1 = roleService.selectById(id);
        model.addAttribute("role",role1);
        return "role/toUpdate";
    }

    //角色数据修改后填充至数据库
    @RequestMapping("updateRoleById")
    @ResponseBody
    public boolean updateRoleById(Role1 role, @RequestParam("mids") Integer[] mids){
        return roleService.updateRoleById(role, mids);
    }
}
